package com.example.myapplication.ui.wr;

import androidx.appcompat.widget.Toolbar;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.databinding.WrFragmentBinding;
import com.example.myapplication.pojo.Article;
import com.example.myapplication.pojo.SingleUser;
import com.example.myapplication.ui.home.HomeActivity;
import com.example.myapplication.ui.login.MainActivity;
import com.example.myapplication.utils.MySqliteHelper;

import java.util.Date;

public class WrFragment extends Fragment {

    private WrViewModel mViewModel;
    private WrFragmentBinding binding;
    private Integer choose;
    private Integer id=0;
    private Integer all=0;
    private String TAG="WrFragment";

    public static WrFragment newInstance() {
        return new WrFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {


        mViewModel = new ViewModelProvider(this).get(WrViewModel.class);
        binding = DataBindingUtil.inflate(inflater, R.layout.wr_fragment, container, false);
        binding.setData(mViewModel);
        Bundle bundle=getActivity().getIntent().getExtras();

        choose= bundle.getInt("choose");
        if(choose==2){
            id=bundle.getInt("id");
            Log.d(TAG, "onCreateView:"+id.toString());
            all=bundle.getInt("all");
            Find();
        }
        binding.toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });

        binding.toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                // 保存
                if(item.getItemId()==R.id.menu_save){
                    Save();
                }
                return false;
            }
        });
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    /**
     * 存储
     */
    public void Save(){
        // 如果文章为空就直接 返回
        if(TextUtils.isEmpty(binding.info.getText().toString())){
            return;
        }
        MySqliteHelper mySqliteHelper=new MySqliteHelper(getActivity());
        SQLiteDatabase db =mySqliteHelper.getReadableDatabase();
        ContentValues cValue = new ContentValues();
        if(choose==1){
            cValue.put("name",SingleUser.getInstance().getUser().getName());
            cValue.put("id",System.currentTimeMillis()%100000);
            cValue.put("date",new Date(System.currentTimeMillis()).toString());
            cValue.put("information",binding.info.getText().toString());
            //调用insert()方法插入数据
            db.insert("article",null,cValue);
            Toast.makeText(getActivity(), "存储成功", Toast.LENGTH_SHORT).show();
        }else if(choose==2){
            // 修改数据
            cValue.put("information",binding.info.getText().toString());
            db.update("article",cValue,"id=?",new String[]{id.toString()});
            Toast.makeText(getActivity(), "修改成功", Toast.LENGTH_SHORT).show();
        }
        db.close();
        Intent intent = new Intent(getContext(), HomeActivity.class);
        startActivity(intent);
    }

    public void Find(){

        MySqliteHelper mySqliteHelper=new MySqliteHelper(getContext());
        SQLiteDatabase readableDatabase =mySqliteHelper.getReadableDatabase();
        //查找当前哟用户所有的文章
        Cursor cursor = readableDatabase.query("article",new String[]{"id","date","name","information"},"id= ?",new String[]{String.valueOf(id)},null,null,null);
        if(cursor.getCount()>0){
            Article article=new Article();
            while (cursor.moveToNext()){
                article.setId(cursor.getInt(cursor.getColumnIndex("id")));
                article.setDate(new Date(cursor.getString(cursor.getColumnIndex("date"))));
                article.setName(cursor.getString(cursor.getColumnIndex("name")));
                article.setInformation(cursor.getString(cursor.getColumnIndex("information")));
            }
            binding.info.setText(article.getInformation());
            binding.toolbar.setTitle("修改文章");
        }else{
            Toast.makeText(getContext(), "对不起你找不到文章", Toast.LENGTH_SHORT).show();
            choose=1;
        }
        cursor.close();
        readableDatabase.close();
    }
}